package cn.com.dbq.demo.jenkins.common;

import cn.com.dbq.demo.jenkins.constant.CommonErrorCodes;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.Objects;

/**
 * @Description
 * @Author xq
 * @Date 2025/1/18 20:44
 **/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result<T> implements Serializable {
    /**
     * 返回结果是否包含status
     *
     * @ignore
     */
    public static boolean STATUS_VISIBLE = true;

    /**
     * 结果编码，0-成功；其它-失败
     *
     * @required
     */
    protected Integer code;

    /**
     * 结果消息
     */
    protected String msg;

    /**
     * 结果数据
     */
    protected T data;

    /**
     * 请求ID
     */
    protected String requestId;

    /**
     * RPC调用链信息
     *
     * @ignore
     */
    protected String errorInfo;

    /**
     * 结果状态
     */
    public Integer getStatus() {
        if (!STATUS_VISIBLE) {
            return null;
        }

        if (code == CommonErrorCodes.SUCCESS) {
            return 1;
        }
        if (code == CommonErrorCodes.FAILURE) {
            return 0;
        }
        return code;
    }

    public boolean success() {
        if (Objects.equals(this.code, CommonErrorCodes.SUCCESS)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean fail() {
        return !success();
    }

}


